capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local manasi "";
local output "";
local log "";
local data "";
local earn "";

log using "`log'\restat_analysis_summstats.log", replace;

local exp "cdr04";

/* 
Manasi Deshpande, 01/08/2013
Summary statistics for full SSI population
*/

use "`earn'\kids_complier_all0405_earn.dta", clear;


/* ******************* */
/* **** DATA PREP **** */
/* ******************* */

/* Earnings */
order n_hhearns0206_*;

/* Parental earnings and income before CDR */
forval  yr=1985(1)2011 {;
	summ n_hhunearninc0206_`yr', det;
	gen n_tothhinc0206_`yr'=n_hhearns0206_`yr'+n_hhunearninc0206_`yr' if n_hhearns0206_`yr'!=.;
	summ n_tothhinc0206_`yr', det;
};

foreach stub in hhearns0206 tothhinc0206 {;
	egen n_`stub'pre=rowmean(n_`stub'_1992-n_`stub'_2001);
};

/* YOB */
summ dobyy, det;
scalar dobyy_med=r(p50);
scalar list;
gen byte dobyy_old=(dobyy<dobyy_med);

/* First age */
summ n_firstage, det;
scalar firstage_med=r(p50);
scalar list;
gen byte firstage_old=(n_firstage>firstage_med);

/* Family structure */
gen byte n_fam_singmom=(toa=="DM" | toa=="BM");
gen byte n_fam_nopar=(toa=="DC" | toa=="BC");
gen byte n_fam_oldpar=(par1_dobyy<1975 & par1_dobyy!=.);
gen byte n_fam_youngpar=(par1_dobyy>=1975 & par1_dobyy!=.);

/* Diagnosis */
destring dibdig, replace;
gen byte n_diag1_mentint=(dibdig>=3170 & dibdig<3200);
gen byte n_diag1_mentoth=(n_diag1_mental & !n_diag1_mentint);

/* Create pre-treatment outcomes discrete variables */
foreach stub in
	totapp hhunearnwokid hhearns tothhinc 
{;
	summ n_`stub'0206pre, det;
	scalar `stub'_med=r(p50);
	scalar list;
	gen byte above_`stub'=(n_`stub'0206pre>`stub'_med);
};

	summ n_combpaypre, det;
	scalar combpay_med=r(p50);
	scalar list;
	gen byte above_combpay=(n_combpaypre>combpay_med);
	

/* ********************************** */
/* **** CHARACTERISTICS ANALYSIS **** */
/* ********************************** */

/* ESTIMATE AVERAGES OVER ENTIRE POPULATION */
foreach var of varlist 
	n_diag1_skin n_diag1_gu n_diag1_circ n_diag1_inj n_diag1_dig n_diag1_musc 
	n_diag1_blood n_diag1_neo n_diag1_infec n_diag1_endo n_diag1_ill n_diag1_resp 
	n_diag1_sens n_diag1_cong n_diag1_nat n_diag1_nerv
	n_diag1_mentint n_diag1_mentoth n_diag1_mental
	above_hhunearnwokid above_totapp above_tothhinc above_hhearns above_combpay
	n_dibmdr_7 n_dibmdr_3 n_dibmdr_99  
	n_fam_nopar n_fam_singmom dobyy_old firstage_old n_male
{;
	summ `var';
	scalar define Epop_`var'=r(mean);
};

scalar list;


/* ************************* */
/* **** FREQUENCY COUNT **** */
/* ************************* */
foreach var of varlist 
	n_male firstage_old dobyy_old n_fam_singmom n_fam_nopar
	n_dibmdr_99 n_dibmdr_3 n_dibmdr_7
	above_combpay above_hhearns above_tothhinc above_totapp above_hhunearnwokid
	n_diag1_mental n_diag1_mentoth n_diag1_mentint
	n_diag1_nerv n_diag1_nat n_diag1_cong n_diag1_sens
	n_diag1_resp n_diag1_ill n_diag1_endo n_diag1_infec n_diag1_neo n_diag1_blood
	n_diag1_musc n_diag1_dig n_diag1_inj n_diag1_circ n_diag1_gu n_diag1_skin
	{;
	count if `var';
};

log close;
